<template>
	<view>
		<uni-popup :show="props.value" type="bottom" @change="change">
			<view class="popup-content d-flex flex-column">
				<view class="d-flex justify-content-end">
					<image src="/static/images/common/clousex-big.png" class="close_btn" @tap="close"></image>
				</view>
				<view class="d-flex flex-column justify-content-between">
					<view class="d-flex flex-column">
						<view class="font-size-extra-lg font-weight-bold text-color-base mb-30">欢迎登陆源社区~</view>
						<view class="font-size-base text-color-assist">登录后消费可获取积分，享受更好的服务体验</view>
					</view>
					<view class="d-flex flex-column margin-top-xs">
						<button class="cu-btn round lg bg-menu text-white text-size-32"
							@click="handleChekcLogin">一键授权登录</button>
						<!-- 	<button  :disabled="isLogin" class="cu-btn round lg bg-menu text-white text-size-32"
							lang="zh_CN" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">
							手机号码授权登录
						</button> -->
						<view class="padding-sm no-login text-center padding" @click="close">
							<text class="text-gray text-size-28">暂不登录</text>
						</view>
						<!-- <view class="padding-sm no-login text-center" @click="handleOtherLogin">
							<text class="text-gray text-size-28">手机号登录</text>
							<text class="margin-lr text-gray">/</text>
							<text class="text-gray text-size-28">账号密码登录</text>
							<text class="cuIcon-right text-gray margin-left"></text>
						</view> -->
						<view class="text-center margin-lr text-size-14 text-color-assist">
							<checkbox-group @change="checkboxChange">
								<checkbox  value="read" />
								我已阅读
								<text @click="handleAgreement" class="text-blue">《用户服务协议》</text>
								<text @click="handleAgreementprivacy" class="text-blue">《隐私协议》</text>
							</checkbox-group>
							已阅读并同意

						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted } from 'vue';
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import { useSettingStore } from '@/stores/index';
	import { accountInfo } from '@/common/api/user';
	import { login } from '@/common/api/login';
	const emit = defineEmits(['change']);
	const setting = useSettingStore();
	const visible = ref('');
	const isLogin = ref(false)
	const canIUse = uni.canIUse('button.open-type.getUserInfo');
	const props = defineProps({
		value: {
			type: Boolean,
			default: false
		}
	});
	const change = ({ show }) => {
		//emit('change', show);
	};
	onMounted(() => {
		uni.getStorage({
			key: 'inviteUserId',
			success: (res : any) => {
				setting.inviteUserId = res.data;
			},
			fail: () => {
		
			}
		})
		//handleWxLogin('')
	})
	const close = () => {
		// setting.token = ''
		// uni.removeStorageSync('token');
		// uni.removeStorage({
		// 	key: 'token'
		// })
		emit('change', false);
	};
	const handleOtherLogin = () => {
		uni.navigateTo({
			url: '/views/login/phonelogin'
		});
	};
	const isRead = ref(false);
	const checkboxChange = (e : any) => {
		isRead.value = e.detail.value[0] == 'read';
	};
	const handleAgreement = () => {
		uni.navigateTo({
			url: '/views/other/agreement'
		});
	};
	const handleAgreementprivacy = () => {
		uni.navigateTo({
			url: '/views/other/agreementprivacy'
		});
	};
	const handleChekcLogin = () => {
		if (!isRead.value) {
			uni.showToast({
				title: '请勾选阅读协议',
				duration: 2000,
				icon: 'error'
			});
			return
		}
		handleWxLogin('')
	}
	const onUserInfo = () => {
		isLogin.value = true
		accountInfo({}).then((res : any) => {
			if (res.code == 1000) {
				setting.userInfo = res.data;
				uni.setStorage({
					key:'userinfo',
					data:JSON.stringify(res.data)
				})
				setting.isLogin = true;
				isLogin.value = false
				if (setting.housing.communityId == 0) {
					uni.navigateTo({
						url: '/views/other/bindshop'
					});
				} else {
					emit('change', true);
				}
			} else {
				isLogin.value = false
			}
		});
	};
	/**
		 * 微信授权code
		 */
	const handleWxLogin = (code : any) => {
		isLogin.value = true
		uni.login({
			provider: 'weixin',
			success: res => {
				onWxLogin(res.code, code);
			}, fail:(err)=> {
				console.log(err)
				isLogin.value = false
			}
		});
	};
	/**
	 * 获取微信手机号code
	 */
	const decryptPhoneNumber = (e : any) => {
		handleWxLogin(e.detail.code)
	};
	const onWxLogin = (code : string, phonecode : string) => {
		isLogin.value = true
		login({
			loginMethod: 'wechat',
			wechatCode: code,
			phoneCode: phonecode,
			inviteUserId:setting.inviteUserId
		}).then((res : any) => {
			uni.hideLoading();
			if (res.code == 1000) {
				setting.onLogin(res.data.token);
				setting.token = res.data.token;
				onUserInfo();
			} else {
				isLogin.value = false
				uni.showToast({
					title: '登录失败',
					duration: 2000
				});
			}
		});
	};
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #ffffff;
		border-radius: 24rpx 24rpx 0 0;
		height: 50vh;
		padding: 50rpx 40rpx;
		z-index: 999;
		padding-bottom: 5vh;
	}

	.close_btn {
		width: 40rpx;
		height: 40rpx;
	}
</style>